using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class GUI_Webpage_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            RoleBll roleBll = new RoleBll();
            roleBll.LoadRole(drpRole);
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {        
        UserInfo userInfo = new UserInfo();
        UserBll userBll = new UserBll();
        CheckTextRequired();
        userInfo.UserID = txtUserID.Text;
        userInfo.Password = txtPassword.Text;
        userInfo.RoleID = Int32.Parse(drpRole.SelectedValue);

        if (userBll.CreateUser(userInfo)>0)
        {            
            LoadData();
            DeleteBlank();
        }
    }
    protected void grvUser_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grvUser.EditIndex = (int)e.NewEditIndex;
        LoadData();
    }
    protected void grvUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grvUser.EditIndex = -1;
        DeleteBlank();
        LoadData();
    }
    protected void grvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UserInfo userInfo = new UserInfo();
        UserBll userBll = new UserBll();

        userInfo.UserID = grvUser.DataKeys[e.RowIndex].Value.ToString();
        
        userBll.DeleteUser(userInfo);
        DeleteBlank();
        LoadData();
    }
    protected void grvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        UserInfo userInfo = new UserInfo();
        UserBll userBll = new UserBll();

        userInfo.UserID = ((TextBox)grvUser.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
        userInfo.Password = ((TextBox)grvUser.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        grvUser.EditIndex = -1;

        userBll.UpdateUser(userInfo);
        DeleteBlank();
        LoadData();
    }

    #region Clear all control
    private void DeleteBlank()
    {
        txtUserID.Text = "";
        txtPassword.Text = "";
    }
    #endregion

    #region Load all user to gridview user
    private void LoadData()
    {
        UserBll userBll = new UserBll();
        try
        {
            SqlDataReader reader = userBll.ListAllUser();
            grvUser.DataSource = reader;
            grvUser.DataBind();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    #endregion

    #region Check required text
    public void CheckTextRequired()
    {
        if (txtUserID.Text == "" & txtPassword.Text == "")
        {
            lblErrorUserID.Text = "UserID is required";
            lblErrorPassword.Text = "Password is required";
        }
        else if (txtUserID.Text == "")
        {
            lblErrorUserID.Text = "UserID is required";
        }
        else if (txtPassword.Text == "")
        {
            lblErrorPassword.Text = "Password is required";
        }
    }
    #endregion
}